/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.order.client  
 */
package com.apache.client;

import java.util.HashMap;
import java.util.Map;

import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.rpc.client.RpcProtoclClient;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;

/**
 * description:  订单引擎客户端
 * @author dyhou 创建时间：2016-5-12  
 */
public class OrderCoreClient {

	private static OrderCoreClient instance;
	private static final String serviceName = "orderService";

	private OrderCoreClient() {
	}

	public static OrderCoreClient newInstance() {
		if (null == instance) {
			instance = new OrderCoreClient();
		}
		return (instance);
	}

	/**
	 * description:  保存订单
	 * @param map
	 * @param orderEngineUrl 订单引擎访问地址
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultMsg saveOrder(Map<String, String> params, String productName, LoginUser loginUser) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		if (StrUtil.isNull(params.get("orderStatus"))) {
			params.put("orderStatus", "1");
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		Map map = new HashMap();
		map.put("orderId", params.get("orderId"));
		buildLogMessage(loginUser.getUserEname(), map, "add", "对商品[" + productName + "]生成购买订单", "下订单");
		map.putAll(params);
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "saveOrder", map,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", entity.getEntity().toString());
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  修改订单状态
	 * @param map
	 */
	public ResultMsg editOrder(Map<String, Object> params, LoginUser loginUser) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		if (StrUtil.isNull(String.valueOf(params.get("orderId")))) {
			msg.setMsg("请指定要操作的订单");
			return msg;
		}
		if (null != loginUser) {
			params.put("userEname", loginUser.getUserEname());
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "editOrderStatus", params,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", entity.getMessage());
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  删除订单
	 * @param map
	 */
	public ResultMsg delOrder(Map<String, Object> params, LoginUser loginUser) {
		ResultMsg msg = new ResultMsg("F", "操作失败");
		if (StrUtil.isNull(String.valueOf(params.get("orderId")))) {
			msg.setMsg("请指定要操作的订单");
			return msg;
		}
		if (null != loginUser) {
			params.put("userEname", loginUser.getUserEname());
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "delOrder", params,
				getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			msg = new ResultMsg("T", entity.getMessage());
		} else {
			msg.setMsg(entity.getMessage());
		}
		return msg;
	}

	/**
	 * description:  查询订单列表
	 * @param map
	 * @param orderEngineUrl 订单引擎访问地址
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultEntity orderList(Map params, LoginUser loginUser, String type) {
		if (null == params) {
			params = new HashMap<String, Object>();
		}
		if ("buy".equals(type)) {
			params.put("buyUserEname", loginUser.getUserEname());
		} else if ("sell".equals(type)) {
			params.put("sellUserEname", loginUser.getUserEname());
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "orderList", params,
				getInterfaceRegister());
		return entity;
	}

	/**
	 * description:  查看订单详情
	 * @param map
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public long countOrder(Map params) {
		ResultEntity entity = new ResultEntity();
		if (null == params) {
			return 0;
		}
		String statuses = String.valueOf(params.get("statuses"));
		if (StrUtil.isNotNull(statuses)) {
			String str[] = statuses.split(",");
			String strs = "";
			for (int i = 0; i < str.length; i++) {
				if (i == str.length - 1) {
					strs += "'" + str[i] + "'";
				} else {
					strs += "'" + str[i] + "',";
				}
			}
			params.remove("orderStatus");
			params.put("orderStatuses", strs);
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		entity = RpcProtoclClient.getInstance().doService(serviceName, "countOrder", params, getInterfaceRegister());
		if ("true".equals(entity.getResult()) && StrUtil.isNotNull(String.valueOf(entity.getEntity()))) {
			return Long.valueOf(String.valueOf(entity.getEntity()));
		}
		return 0;
	}

	/**
	 * description:  查看订单详情
	 * @param map
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultEntity viewOrder(Map params) {
		ResultEntity entity = new ResultEntity();
		if (null == params) {
			entity.setResult("false");
			entity.setMessage("请选择要查看的订单信息");
			return entity;
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		entity = RpcProtoclClient.getInstance().doService(serviceName, "orderDetail", params, getInterfaceRegister());
		return entity;
	}

	/**
	 * description:  查询日志详情信息
	 * @param map
	 * @param orderEngineUrl 订单引擎访问地址
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultEntity viewLogMessage(Map params) {
		ResultEntity entity = new ResultEntity();
		if (null == params) {
			entity.setResult("false");
			entity.setMessage("请选择要查看的日志信息");
			return entity;
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		entity = RpcProtoclClient.getInstance().doService(serviceName, "logDetail", params, getInterfaceRegister());
		return entity;
	}

	/**
	 * description:  查询日志列表信息
	 * @param map
	 * @param orderEngineUrl 订单引擎访问地址
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ResultEntity logMessageList(Map params) {
		if (null == params) {
			params = new HashMap<String, Object>();
		}
		params.put("sysPass", ToolsUtil.getInstance().getValueByKey("push_to_dwusoft_pass"));
		ResultEntity entity = RpcProtoclClient.getInstance().doService(serviceName, "logList", params,
				getInterfaceRegister());
		return entity;
	}

	private void buildLogMessage(String userEname, Map params, String logType, String msg, String title) {
		params.put("logUserEname", userEname);
		params.put("logType", logType);
		params.put("logMessage", msg);
		params.put("logTitle", title);
	}

	private InterfaceRegister getInterfaceRegister() {
		InterfaceRegister register = new InterfaceRegister();
		String reqUrl = ToolsUtil.getInstance().getValueByKey("order_engine_url");
		String infoUrl = reqUrl + "/order/service/doService.action";
		if (reqUrl.startsWith("https:")) {
			register.setCallType("https");
			register.setAddress(infoUrl);
		} else if (reqUrl.startsWith("http:")) {
			register.setCallType("http");
			register.setAddress(infoUrl);
		} else {
			String port = ToolsUtil.getInstance().getValueByKey("order_engine_port");
			register.setAddress(reqUrl);
			register.setPort(port);
			register.setCallType("socket");
		}
		return register;
	}
}
